package com.jjh.users.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.service.BaseService;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.PageInfoUtil;
import com.jjh.users.dao.IAMenuDao;
import com.jjh.users.dao.IARoleMenuDao;
import com.jjh.users.pojo.AMenu;
import com.jjh.users.pojo.ARole;
import com.jjh.users.pojo.ARoleMenu;
import com.jjh.users.service.IAuthDbService;
import com.jjh.users.service.IUsersDbService;

/**
 * 权限相关的Service类
 * @author jjh
 *
 */
@Service("authDbService")
public class AuthDbServiceImpl extends BaseService implements IAuthDbService
{
	/*
	 * 菜单dao的实现类
	 * 菜单角色dao的实现类
	 */
	@Autowired
	private IAMenuDao menuDao ;
	
	@Autowired
	private IARoleMenuDao roleMenuDao ;
	
	/*  user的service */
	@Autowired
	private IUsersDbService usersDbService ;
	
	public void setUsersDbService(IUsersDbService usersDbService)
	{
		this.usersDbService = usersDbService;
	}
	/*=========菜单表操作开始===========*/

	@Override
	public JSONObject saveOneMenuService(AMenu menu)
	{
		JSONObject resultJson = new JSONObject();
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		/* 调用dao保存到数据库 */
		int res = this.menuDao.saveOneDao(menu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), menu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneMenuService(AMenu menu)
	{
		JSONObject resultJson = new JSONObject();
		menu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.menuDao.updateOneDao(menu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), menu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneMenuService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.menuDao.deleteBatch(condMap);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public AMenu findOneMenuService(Map<String, Object> condMap)
	{
		if(condMap != null && condMap.isEmpty())
		{
			ConstatFinalUtil.SYS_LOGGER.info(ConstatFinalUtil.INFO_QUERY,condMap);
			return null ;
		}
		return this.menuDao.findOneDao(condMap);
	}

	@Override
	public List<AMenu> findCondListMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{

		/* 专门处理%的问题 */
		if(condMap.get(InfoEnum.FINA_KEYWORD.getInfo()) != null && !"".equalsIgnoreCase(condMap.get(InfoEnum.FINA_KEYWORD.getInfo()) + ""))
		{
			condMap.put(InfoEnum.FINA_KEYWORD.getInfo(), "%" + condMap.get(InfoEnum.FINA_KEYWORD.getInfo()) + "%") ; 
		}
		
		/* 处理分页 */
		if(pageInfoUtil != null )
		{
			/* 分页 */
			Page<AMenu> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 调用查询语句 */
			List<AMenu> list = this.menuDao.findCondListDao(condMap);
			/* 获取总条数 */
			long total = page.getTotal();
			pageInfoUtil.setTotalRecord(Long.valueOf(total).intValue());
			return list ; 
		}
		
		return this.menuDao.findCondListDao(condMap);
	}

	/*=========菜单表操作结束===========*/ 
	
	
	/*=========菜单角色表操作开始===========*/ 
	@Override
	public JSONObject saveOneRoleMenuService(ARoleMenu roleMenu)
	{
		JSONObject resultJson = new JSONObject();
		roleMenu.setCreateTime(new Date());
		roleMenu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.saveOneDao(roleMenu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), roleMenu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneRoleMenuService(ARoleMenu roleMenu)
	{
		JSONObject resultJson = new JSONObject();
		roleMenu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.updateOneDao(roleMenu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), roleMenu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneRoleMenuService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.deleteBatch(condMap);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public ARoleMenu findOneRoleMenuService(Map<String, Object> condMap)
	{
		if(condMap != null && condMap.isEmpty())
		{
			ConstatFinalUtil.SYS_LOGGER.info(ConstatFinalUtil.INFO_QUERY,condMap);
			return null ;
		}
		return this.roleMenuDao.findOneDao(condMap);
	}

	@Override
	public List<ARoleMenu> findCondListRoleMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 专门处理%的问题 */
		if(condMap.get(InfoEnum.FINA_KEYWORD.getInfo()) != null && !"".equalsIgnoreCase(condMap.get(InfoEnum.FINA_KEYWORD.getInfo()) + ""))
		{
			condMap.put(InfoEnum.FINA_KEYWORD.getInfo(), "%" + condMap.get(InfoEnum.FINA_KEYWORD.getInfo()) + "%") ; 
		}
		
		/* 处理分页 */
		if(pageInfoUtil != null )
		{
			/* 分页 */
			Page<ARoleMenu> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 调用查询语句 */
			List<ARoleMenu> list = this.roleMenuDao.findCondListDao(condMap);
			/* 获取总条数 */
			long total = page.getTotal();
			pageInfoUtil.setTotalRecord(Long.valueOf(total).intValue());
			return list ; 
		}
		
		return this.roleMenuDao.findCondListDao(condMap);
	}
	
	/*=========菜单角色表操作结束===========*/ 

	@Override
	public List<AMenu> findCondListTreeMenuService(int id, String operType)
	{
		List<AMenu> resultList = new ArrayList<AMenu>();
		if(id == 0)
		{
			/* 如果id是0,查询所有的一级节点 */
			Map<String,Object> condMap = new HashMap<String,Object>();
			condMap.put("parentId", id);
			List<AMenu> rootList = this.findCondListMenuService(null, condMap);
			for(AMenu menu : rootList)
			{
				/* 向下找 */
				this.findCondListTreeMenuDownServiceUtil(menu.getId(), 0, resultList);
				
			}
		}else
		{
			if("up".equalsIgnoreCase(operType))
			{
				/* 向上找得先确定id */
				this.findCondListTreeMenuUpServiceUtil(id, 0, resultList);
				/* 反转 */
				Collections.reverse(resultList);
			}else
			{
				/* 向下找 */
				this.findCondListTreeMenuDownServiceUtil(id, 0, resultList);
			}
		}
		return resultList ;
	}
	
	/**
	 * 树形结构的工具类  递归 向上找 
	 * @param id
	 * @param leval
	 * @param resultList
	 */
	private void findCondListTreeMenuUpServiceUtil(int id , int leval , List<AMenu> resultList)
	{
		/* 根据id查询当前节点 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AMenu menu = this.findOneMenuService(condMap);
		if(menu != null)
		{
			/* 这句话很关键是为了存储结果 */
			resultList.add(menu);
			/* 在往下*/
			AMenu menuTemp = menu.getParentMenu();
			if(menuTemp != null)
			{
				findCondListTreeMenuUpServiceUtil(menuTemp.getId(), leval + 1 , resultList);
			}
		}
	}
	
	/**
	 * 树形结构的工具类  递归  向下找 
	 * @param id
	 * @param leval
	 * @param resultList
	 */
	private void findCondListTreeMenuDownServiceUtil(int id , int leval , List<AMenu> resultList)
	{
		/* 根据id查询当前的节点 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AMenu menu = this.findOneMenuService(condMap);
		if(menu != null)
		{
			StringBuilder sb = new StringBuilder();
			for(int i = 0 ; i < leval ; i ++)
			{
				sb.append("|---->");
			}
			//System.out.println("==循环==" + sb.toString() + menu.getName());
			menu.setTreeName(sb.toString() + menu.getName());
			/* 这句话很关键为了存储查询的结果 */
			resultList.add(menu);
			/* 在往下讨论 
			 * 查循子节点
			 * */
			List<AMenu> childrenMenu = menu.getChildMenuList();
			for (AMenu menuTemp : childrenMenu)
			{
				findCondListTreeMenuDownServiceUtil(menuTemp.getId() , leval + 1 , resultList);
			}
		}
	}
}
